use image::GenericImageView;
use simple_excel_writer::*;

fn main() {
    let im = image::open(r"D:\document\HuaweiPhotos\me_small.png").unwrap();
    let (w, h) = im.dimensions();
    println!("w:{} h:{}", w, h);

    let color_map = "MNHQ$OC?7>!:-;. ";
    let mut wb = Workbook::create("a.xlsx");
    let mut sheet = wb.create_sheet("sheet1");

    for _ in 0..w {
        sheet.add_column(Column { width: 2.5 });
    }

    wb.write_sheet(&mut sheet, |sw| {
        for y in 0..h {
            let mut row = Row::new();
            for x in 0..w {
                let color = im.get_pixel(x, y);
                let r = color.data[0] as u32;
                let g = color.data[1] as u32;
                let b = color.data[2] as u32;
                let value = ((r + g + b) / 3 / 16) as usize;
                if let Some(c) = color_map.chars().nth(value) {
                    row.add_cell(CellValue::String(c.to_string()));
                }
            }
            sw.append_row(row)?;
        }
        Ok(())
    }).expect("error");
    wb.close().expect("error");
}
